<?php

/*===============微信商城=================*/
/*----------------------------------------*/
/* $Author: 疯中追风                      */
/*----------------------------------------*/
/* $Time: 2017/10/11 9:12                 */
/*----------------------------------------*/
/*=================首页===================*/

namespace Pc\Controller;
use Think\Controller;
class IndexController extends Controller{

    public function index(){
         //实例化模板
         //商品表
         $goods = M('Goods') -> select();
         //商品父类
         $goods_type = M('Goods_type') -> select();
         //轮播商品表
         $goods_carousel = M('goods_carousel') -> limit(4,7) -> select();
         //新品展示区
         $is_new_goods   = M('Goods') -> where(array('is_new_pc'=>1)) -> select();
         //分类父类
         $goods_type_parent = M('Goods_type_parent') -> select();
         //广告栏
         $banner = M('Goods_banner') -> select();
         //获取当前城市
         $city  =  get_city();

         $this -> assign('goods',$goods);
         $this -> assign('banner',$banner);
          $this -> assign('city',$city);
         $this -> assign('goods_type',$goods_type);
         $this -> assign('goods_carousel',$goods_carousel);
         $this -> assign('is_new_goods',$is_new_goods);
         $this -> assign('goods_type_parent',$goods_type_parent);
         $this -> display();
    }

    //随机查询商品并输出首页
     public function ajaxGetMore(){
 		/*实力化模板*/
    	$goods = M('Goods');
    	//声明一个存放随机商品的一个数组
    	$love_goods = array();
    	//随机出来6个商品
    	for($i = 0;$i < 6;$i++){
    		$rand = mt_rand(1,17);
    		$love_good = $goods -> where("id = $rand") -> find();
    		$love_goods[$i] = $love_good;
    	}

    	$this -> assign('love_goods',$love_goods);
    	$this -> display();
    }

    public function  weixin_interface(){
        //首先判断session中是否存在用户名和头像。如果存在则不用使用oauth请求access_token
            $date = getdate(date("U"));
            $current_date = $date[year].'-'.($date[mon]+1).'-'.$date[mday]." "."  ".$date[hours].':'.$date[minutes].':'.$date[seconds];
            //获取code
            $code  = $_GET['code'];

            //公众号唯一id
            $appid = 'wx5b3db39546bb1360';
            $appsecret = '9ff6efc83ad1e67a07403fe402ccd1b8';

            //通过code获取access_token
            $get_access_token = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
            $text = file_get_contents($get_access_token);

            //刷新access_token
            $refresh_token = json_decode($text,true)['refresh_token'];
            $text2 = file_get_contents('https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$refresh_token);
            //将json字符串转换为数组
            $access_token =  json_decode($text2,true)['access_token'];
            //用户唯一id
            $openid       =  json_decode($text,true)['openid'];

            //获取微信个人信息
            $personInfo   =  file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN');

            $name     = json_decode($personInfo,true)['nickname'];
            $imageUrl = json_decode($personInfo,true)['headimgurl'];
            //将昵称和头像地址放入到cookie里面
            cookie('name',$name,7000);
            cookie('imageUrl',$imageUrl,7000);
            //首先判断是否存在用户
            $result = M("User") -> where(array('nickname'=>$name,'head_pic'=>$imageUrl)) -> find();
            
            if(!$result){   

                //获取ip地址
                $ip = get_client_ip();

                //存入数据库
                M("User") -> add(array('oauth'=>'weixin','head_pic'=>$imageUrl,'nickname'=>$name,'last_login'=>$current_date,'last_ip'=>$ip));
                $id = M("User") -> where(array('nickname'=>$name)) -> find();
                cookie('id',$id['user_id'],7000);
                redirect(U('index'));
            }else{
                cookie('id',$result['user_id'],7000);
                redirect(U('index'));
            }
    }

    //获取签名信息
    public function  get_ticket(){
        $get_id = I('get.id');

        if(cookie('ticket2')==null){
            //获取access_token,注意；这里的access_token和通过网页获取授权的access_token是不一致的
            $json_text = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx5b3db39546bb1360&secret=9ff6efc83ad1e67a07403fe402ccd1b8");
            $access_token2 = json_decode($json_text,true)['access_token'];
            //请求地址，获取带有签名的json字符串
            $get_ticket_json = file_get_contents("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$access_token2."&type=jsapi");
            //获取签名
            $ticket2 = json_decode($get_ticket_json,true)['ticket'];
            //将ticket存入session里面
            //cookie('ticket',$ticket,7000);
            //4.使用sha1()方法进行签名.
            cookie('ticket2',$ticket2,7000);
            $signature = sha1('jsapi_ticket='.$ticket2.'&noncestr=wsssssfdfdaf&timestamp=1510191818&url=http://www.jsyuanma.top/index.php/Home/GoodsInfo/goodsInfo/id/'.$get_id);
            //返回签名
            echo $signature;
        }else{
            //将ticket存入session里面
            //cookie('ticket',$ticket,7000);
            //4.使用sha1()方法进行签名.
            //判断是否带后缀".html"
            $signature = sha1('jsapi_ticket='.cookie('ticket2').'&noncestr=wsssssfdfdaf&timestamp=1510191818&url=http://www.jsyuanma.top/index.php/Home/GoodsInfo/goodsInfo/id/'.$get_id);
            //返回签名
            echo $signature;
        }
        /*
        }
PaDkb9Sg4OUxklWiRMX-XZ754SyI1p87Kk8ONUi-OQOMAQxAfG_WJm4z0ikOhdX6kuT0jv9WZD3tie_mFVILeA61xF1svs2pyJXRye1BqmQ

https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=xAo-S9cE7361jRN_WjXjc8x5G1fVzwPq6Ocs-bDqcHrJacORHxCFXMOCdyQuX94jtmPmx6Jj1UX9pyHduZnktS3UfExHyUKIZi-rYxKpAKY&type=jsapi
*/
    }

    //错误提示信息
    public function _empty(){
        echo "您访问的页面不存在！";
    }

}

/*
INSERT INTO `sp_goods`(`type_id`, `goods_number`, `goods_name`, `market_price`, `local_price`, `desc_`, `descs_id`, `goods_image`, `is_grounding`, `is_free_post`, `is_hot`, `is_new`, `is_show_pc_model`, `new_goods_img`, `sales`, `goods_stock`,
 `color_type`, `goods_size`) VALUES
(2,'000ff19','优品12支装黑色中性笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff20','彩色磨砂笔盒收纳文具盒','7.5',6,"实惠好用文具",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(4,'000ff21',' 冬己32K裸脊本硬抄本记事本','35.00',29,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff19','晨光速干中性笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','晨光萌宠蛋修正带','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','晨光全针管中性笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','晨光简薄计算器','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','晨光政区地球仪','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','晨光创意笔试剪刀','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','晨光无用之诗宝珠笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8");
(2,'000ff18','优品12支装黑色中性笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','优品12支装黑色中性笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','优品12支装黑色中性笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','优品12支装黑色中性笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','优品12支装黑色中性笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','优品12支装黑色中性笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','优品12支装黑色中性笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8"),
(2,'000ff18','优品12支装黑色中性笔','73.00',57,"实用",18,'/Public/Moble_home/images/1-1.jpg',1,0,0,0,1,"ssss",568,300,"白色_蓝色_绿色_青色","9*9_8*5_10*8");
*/
/*
INSERT INTO `sp_goods_carousel`(`carouse_id`, `goods_id`, `goods_carousel_img`) VALUES (5,17,"/Public/Moble_home/images/1.jpg"),
(6,18,"/Public/Moble_home/images/2.jpg"),
(7,17,"/Public/Moble_home/images/3.jpg");
*/
